<?php
namespace app\api\model;
use think\Model;

class Policy extends Model{  
    public function allList($filter = null){
        $where = [];
        $page = 1;
        $pageCount = 10;
        if(!empty($filter)){
            //指定id查询
            if(!empty($filter['id'])){
                $where['id'] = $filter['id'];
            } 
            if($filter['type']){
                $where['type'] = $filter['type'];
            }

            if(!empty($filter['date_type']) && $filter['date_type'] == "insurance_date"){
                //按出发日期查询
                if(!empty($filter['start_date']) && !empty($filter['end_date'])){
                    $where['insurance_date'] = [['>=',str_replace("-","",$filter['start_date'])],['<=',str_replace("-","",$filter['end_date'])]];
                }              
            }else{
                //按录入日期查询
                if(!empty($filter['start_date']) && !empty($filter['end_date'])){
                    $where['begdate'] = [['>=',str_replace("-","",$filter['start_date'])],['<=',str_replace("-","",$filter['end_date'])]];
                }
            }

            //页码            
            $page = !empty($filter['page']) ? $filter['page'] : $page;
            $pageCount = !empty($filter['page_count']) ? $filter['page_count'] : $pageCount;     
        }
        $datas = $this->where($where)->page("$page,$pageCount")->order('begdate', 'desc')->select(); 
        $dataCount = $this->where($where)->count(); 
        $datalist = array(
            'datas' => $datas,            
            'dataCount' => $dataCount
        );
        return $datalist;
    }
}